package com.keke.service.bill;

import com.keke.ado.baseDao;
import com.keke.ado.bill.billDao;
import com.keke.ado.bill.billDaoImpl;
import com.keke.pojo.Bill;

import java.sql.Connection;
import java.sql.SQLException;
import java.util.List;

public class BillServiceImpl implements BillService{

    private billDao billDao;

    public BillServiceImpl() {
        this.billDao = new billDaoImpl();
    }

    public boolean add(Bill bill) {
        boolean flag= false;
        Connection connection = null;
        try {
            connection = baseDao.getConnection();
            connection.setAutoCommit(false);
            if(billDao.add(connection,bill)>0){
                flag=true;
            }
            connection.commit();
        } catch (Exception e) {
            e.printStackTrace();
            try {
                System.out.println("=================rollback=================");
                connection.rollback();
            } catch (SQLException ex) {
                ex.printStackTrace();
            }
        }finally {
            baseDao.close(connection,null,null);
        }
        return flag;
    }

    public List<Bill> getBillList(Bill bill,int currentPageNo, int pageSize) {
        Connection connection = null;
        List<Bill> billList = null;
        System.out.println("query productName -----> "+bill.getProductName());
        System.out.println("query productId -----> "+bill.getProviderId());
        System.out.println("query isPayment -----> "+bill.getIsPayment());

        try {
            connection=baseDao.getConnection();
            billList=billDao.getBillList(connection,bill,currentPageNo,pageSize);
        } catch (Exception e) {
            e.printStackTrace();
        }finally {
            baseDao.close(connection,null,null);
        }
        return billList;
    }

    public boolean deleteBillById(String delId) {
        Connection connection=null;
        boolean flag= false;
        try {
            connection=baseDao.getConnection();
            if(billDao.deleteBillById(connection,delId)>0){
                flag=true;
            }
        } catch (Exception e) {
            e.printStackTrace();
        }finally {
            baseDao.close(connection,null,null);
        }
        return flag;
    }

    public Bill getBillById(String id) {
        Bill bill = null;
        Connection connection = null;

        try {
            connection = baseDao.getConnection();
            bill=billDao.getBillById(connection,id);
        } catch (Exception e) {
            e.printStackTrace();
            bill= null;
        }finally {
            baseDao.close(connection,null,null);
        }
        return bill;
    }

    public boolean modifyBill(Bill bill) {
        Connection connection = null;
        boolean flag= false;

        connection = baseDao.getConnection();
        try {
            if(billDao.modifyBill(connection,bill)>0){
                flag=true;
            }
        } catch (Exception e) {
            e.printStackTrace();
        }finally {
            baseDao.close(connection,null,null);
        }
        return flag;
    }

    //查询记录数
    public int getBillCount( String productName,int ProviderId,int isPayment) {
        Connection connection = null;
        int count=0;
        try {
            connection=baseDao.getConnection();
            count = billDao.getBillCount(connection, productName, ProviderId,isPayment);

        } catch (SQLException e) {
            e.printStackTrace();
        }finally {
            baseDao.close(connection,null,null);
        }
        return count;
    }
}
